
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 12/10/2012 19:18:39
-- Generated from EDMX file: D:\server-rss\Server\RssFeedModel.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_item_channel]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[items] DROP CONSTRAINT [FK_item_channel];
GO
IF OBJECT_ID(N'[dbo].[FK_registration_channel]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[registrations] DROP CONSTRAINT [FK_registration_channel];
GO
IF OBJECT_ID(N'[dbo].[FK_itemState_item]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[itemStates] DROP CONSTRAINT [FK_itemState_item];
GO
IF OBJECT_ID(N'[dbo].[FK_itemState_user]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[itemStates] DROP CONSTRAINT [FK_itemState_user];
GO
IF OBJECT_ID(N'[dbo].[FK_registration_user]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[registrations] DROP CONSTRAINT [FK_registration_user];
GO
IF OBJECT_ID(N'[dbo].[FK_session_user]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[sessions] DROP CONSTRAINT [FK_session_user];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[channels]', 'U') IS NOT NULL
    DROP TABLE [dbo].[channels];
GO
IF OBJECT_ID(N'[dbo].[items]', 'U') IS NOT NULL
    DROP TABLE [dbo].[items];
GO
IF OBJECT_ID(N'[dbo].[itemStates]', 'U') IS NOT NULL
    DROP TABLE [dbo].[itemStates];
GO
IF OBJECT_ID(N'[dbo].[registrations]', 'U') IS NOT NULL
    DROP TABLE [dbo].[registrations];
GO
IF OBJECT_ID(N'[dbo].[sessions]', 'U') IS NOT NULL
    DROP TABLE [dbo].[sessions];
GO
IF OBJECT_ID(N'[dbo].[users]', 'U') IS NOT NULL
    DROP TABLE [dbo].[users];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'channels'
CREATE TABLE [dbo].[channels] (
    [id] int IDENTITY(1,1) NOT NULL,
    [title] varchar(50)  NULL,
    [description] varchar(max)  NULL,
    [link] varchar(50)  NULL,
    [url] varchar(100)  NOT NULL,
    [date] datetime  NOT NULL,
    [public] bit  NOT NULL
);
GO

-- Creating table 'items'
CREATE TABLE [dbo].[items] (
    [id] int IDENTITY(1,1) NOT NULL,
    [title] varchar(50)  NULL,
    [description] varchar(max)  NULL,
    [date] datetime  NOT NULL,
    [link] varchar(50)  NULL,
    [channel] int  NOT NULL
);
GO

-- Creating table 'itemStates'
CREATE TABLE [dbo].[itemStates] (
    [id] int IDENTITY(1,1) NOT NULL,
    [readState] bit  NOT NULL,
    [item] int  NOT NULL,
    [user] int  NOT NULL
);
GO

-- Creating table 'registrations'
CREATE TABLE [dbo].[registrations] (
    [id] int IDENTITY(1,1) NOT NULL,
    [date] datetime  NOT NULL,
    [channel] int  NOT NULL,
    [user] int  NOT NULL
);
GO

-- Creating table 'sessions'
CREATE TABLE [dbo].[sessions] (
    [id] int IDENTITY(1,1) NOT NULL,
    [date] datetime  NOT NULL,
    [version] int  NOT NULL,
    [user] int  NOT NULL
);
GO

-- Creating table 'users'
CREATE TABLE [dbo].[users] (
    [id] int IDENTITY(1,1) NOT NULL,
    [email] varchar(50)  NOT NULL,
    [password] varchar(50)  NOT NULL,
    [date] datetime  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [id] in table 'channels'
ALTER TABLE [dbo].[channels]
ADD CONSTRAINT [PK_channels]
    PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- Creating primary key on [id] in table 'items'
ALTER TABLE [dbo].[items]
ADD CONSTRAINT [PK_items]
    PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- Creating primary key on [id] in table 'itemStates'
ALTER TABLE [dbo].[itemStates]
ADD CONSTRAINT [PK_itemStates]
    PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- Creating primary key on [id] in table 'registrations'
ALTER TABLE [dbo].[registrations]
ADD CONSTRAINT [PK_registrations]
    PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- Creating primary key on [id] in table 'sessions'
ALTER TABLE [dbo].[sessions]
ADD CONSTRAINT [PK_sessions]
    PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- Creating primary key on [id] in table 'users'
ALTER TABLE [dbo].[users]
ADD CONSTRAINT [PK_users]
    PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [channel] in table 'items'
ALTER TABLE [dbo].[items]
ADD CONSTRAINT [FK_item_channel]
    FOREIGN KEY ([channel])
    REFERENCES [dbo].[channels]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_item_channel'
CREATE INDEX [IX_FK_item_channel]
ON [dbo].[items]
    ([channel]);
GO

-- Creating foreign key on [channel] in table 'registrations'
ALTER TABLE [dbo].[registrations]
ADD CONSTRAINT [FK_registration_channel]
    FOREIGN KEY ([channel])
    REFERENCES [dbo].[channels]
        ([id])
    ON DELETE CASCADE ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_registration_channel'
CREATE INDEX [IX_FK_registration_channel]
ON [dbo].[registrations]
    ([channel]);
GO

-- Creating foreign key on [item] in table 'itemStates'
ALTER TABLE [dbo].[itemStates]
ADD CONSTRAINT [FK_itemState_item]
    FOREIGN KEY ([item])
    REFERENCES [dbo].[items]
        ([id])
    ON DELETE CASCADE ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_itemState_item'
CREATE INDEX [IX_FK_itemState_item]
ON [dbo].[itemStates]
    ([item]);
GO

-- Creating foreign key on [user] in table 'itemStates'
ALTER TABLE [dbo].[itemStates]
ADD CONSTRAINT [FK_itemState_user]
    FOREIGN KEY ([user])
    REFERENCES [dbo].[users]
        ([id])
    ON DELETE CASCADE ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_itemState_user'
CREATE INDEX [IX_FK_itemState_user]
ON [dbo].[itemStates]
    ([user]);
GO

-- Creating foreign key on [user] in table 'registrations'
ALTER TABLE [dbo].[registrations]
ADD CONSTRAINT [FK_registration_user]
    FOREIGN KEY ([user])
    REFERENCES [dbo].[users]
        ([id])
    ON DELETE CASCADE ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_registration_user'
CREATE INDEX [IX_FK_registration_user]
ON [dbo].[registrations]
    ([user]);
GO

-- Creating foreign key on [user] in table 'sessions'
ALTER TABLE [dbo].[sessions]
ADD CONSTRAINT [FK_session_user]
    FOREIGN KEY ([user])
    REFERENCES [dbo].[users]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_session_user'
CREATE INDEX [IX_FK_session_user]
ON [dbo].[sessions]
    ([user]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------